#include "CameraPrecompiled.hxx"
#include "CameraFirstPerson.hxx"

Eifersucht::FirstPersonCamera::FirstPersonCamera() : 
	BasicCamera(),
	m_bRotateWithoutPress(true),
	m_iActiveButtonMask(0x7)
{
}

Void Eifersucht::FirstPersonCamera::OnFrameMove( _In_ Float32 fElapsed )
{
	_Basic_camera_input( true, true );
	_Basic_camera_velocity( fElapsed );

	auto const f3PositionDelta = m_f3Velocity * fElapsed;

	if ((m_iActiveButtonMask & m_iButtonMask) || 
		(m_bRotateWithoutPress))
	{
		auto const fYawDelta = m_f2RotationVelocity.x;
		auto const fPitchDelta = (m_bInvertPitch) ?
			(-m_f2RotationVelocity.y) : 
			(m_f2RotationVelocity.y);

		m_fPitch += fPitchDelta;
		m_fYaw += fYawDelta;

		// Limit pitch for straight up and down
		m_fPitch = MaxOf( -Reign::XM_PIDIV2, m_fPitch );
		m_fPitch = MinOf( Reign::XM_PIDIV2, m_fPitch );
	}
	
	Float32x3Const f3LclUp(0.0f, 1.0f, 0.0f);
	Float32x3Const f3LclAhead(0.0f, 0.0f, 1.0f);
	Float32x4x4Const f4x4Rotation = m_bEnableVerticalMovement ? 
		DirectX::XMMatrixRotationRollPitchYaw( m_fPitch, m_fYaw, 0 ) : 
		DirectX::XMMatrixRotationRollPitchYaw( 0.0f, m_fYaw, 0.0f );

	Float32x3Const f3PositionWorldDelta = DirectX::XMVector3TransformCoord( f3PositionDelta, f4x4Rotation);
	Float32x3Const f3WorldAhead = DirectX::XMVector3TransformCoord(f3LclAhead, f4x4Rotation);
	Float32x3Const f3WorldUp = DirectX::XMVector3TransformCoord(f3LclUp, f4x4Rotation);

	m_f3Eye += f3PositionWorldDelta; // move the eye position
	if ( m_bClipToBoundary ) _Basic_camera_constraine(&m_f3Eye);

	m_f3LookAt = m_f3Eye + f3WorldAhead;
	m_f4x4View = DirectX::XMMatrixLookAtLH(m_f3Eye, m_f3LookAt, f3WorldUp);
	m_f4x4World = DirectX::XMMatrixInverse(nullptr, m_f4x4View);

}